package com.elong.businesstravel.modules.home;

import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.utils.ToolUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.utils.SettingsUtils;
import com.elong.businesstravel.entitys.City;
import com.elong.businesstravel.entitys.Hotel;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.entitys.HotelSearchCondition;
import com.elong.businesstravel.entitys.MyGeoPoint;
import com.elong.businesstravel.entitys.Poi;
import com.elong.businesstravel.modules.home.adapter.HeadHotelSearchResultAdapter;
import com.elong.businesstravel.modules.home.adapter.HotelSearchResultAdapter;
import com.elong.businesstravel.modules.home.baseactivity.BaseHotelResultActivity;

/**
 * 可抵扣的酒店，
 * 
 * @author lenovo
 * 
 */
public class PrepaidHotelSearchResultActivity extends BaseHotelResultActivity {

	private PullToRefreshListView mListView;
	private HotelSearchResultAdapter mHotelSearchResultAdapter;

	/** true表示抵扣列表跳转到map界面 */
	private boolean mPrepaidFlag = false;

	private ViewGroup mNoResultLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel_search_result);

		init();
	}

	@Override
	protected void init() {
		mDestGeoPoint = (MyGeoPoint) getIntent().getSerializableExtra("DestGeoPoint");
		mCurrentHotelSearchCondition = (HotelSearchCondition) getIntent()
				.getSerializableExtra("HotelSearchCondition");
		if (-1 == SettingsUtils.getHotelFull(mContext)) {
			mCurrentHotelSearchCondition.isfull = 1;
		} else {
			mCurrentHotelSearchCondition.isfull = SettingsUtils
					.getHotelFull(mContext);
		}
		mCurrentHotelSearchCondition.product = 1;

		try {
			String poiNameString = getIntent().getStringExtra("PoiName");
			mPrepaidFlag = getIntent().getBooleanExtra("prepaid", false);
			if (!TextUtils.isEmpty(poiNameString)) {
				mCurrentHotelSearchCondition.sorts = Integer
						.parseInt(getIntent().getStringExtra("sorts"));
				Poi poi = new Poi();
				poi.name = getIntent().getStringExtra("PoiName");
				poi.googlelat = getIntent().getDoubleExtra("googlelat", 0);
				poi.googlelng = getIntent().getDoubleExtra("googlelng", 0);
				mCurrentHotelSearchCondition.mSearchConditionPoi = poi;
				mCurrentHotelSearchCondition.mSearchConditionCity = (City) getIntent()
						.getSerializableExtra("LocationCity");
			}
		} catch (Exception e) {
		}

		super.init();

		mNoResultLayout = (ViewGroup) findViewById(R.id.noResultLayout);
		mListView = (PullToRefreshListView) findViewById(R.id.listView);
		mListView.setMode(Mode.PULL_FROM_END);
		mHotelSearchResultAdapter = new HotelSearchResultAdapter(mContext,
				mHotel.mHotelDocs, mCurrentHotelSearchCondition);
		mListView.setAdapter(mHotelSearchResultAdapter);

		setListener();

		getHotelList(false);
	}

	/**
	 * 没有结果
	 */
	private void noResult() {
		mNoResultLayout.setVisibility(View.VISIBLE);
		mListView.setVisibility(View.GONE);
	}

	private void haveResult() {
		mNoResultLayout.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Hotel hotel = (Hotel) intent.getSerializableExtra("Hotel");
		mPrepaidFlag = getIntent().getBooleanExtra("prepaid", false);
		mHotel.mHotelDocs.clear();
		mHotel.mHotelDocs.addAll(hotel.mHotelDocs);
		mSortByDistance = getIntent().getBooleanExtra("SortByDistance", false);
		// mHandler.sendEmptyMessage(0);
		mHandler.sendMessage(mHandler.obtainMessage(0, mSortByDistance));

		setTitleNavBar();
	}

	@Override
	protected void setListener() {

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(mContext, HotelDetailActivity.class);
				intent.putExtra("hotelDocs",
						(HotelDocs) mHotelSearchResultAdapter.getItem(position));
				intent.putExtra("product", mCurrentHotelSearchCondition.product);
				startActivity(intent);
			}
		});

		mListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {

				getHotelList(true);

				// mHandler.sendEmptyMessage(1);
			}
		});

		super.setListener();
	}

	@Override
	protected void updateHotelDate(boolean sortByDistance) {
		// mHandler.sendEmptyMessage(0);

		mHandler.sendMessage(mHandler.obtainMessage(0, sortByDistance));
	}

	@Override
	protected void titleRightOnClick(String textString) {
		Intent intent = new Intent();
		if (MAP.equals(textString)) {
			intent.setClass(mContext, HotelSearchResultMapActivity.class);
			intent.putExtra("activityTag", LIST);
			intent.putExtra("sort", mHotelSort);
			intent.putExtra("HotelSearchCondition",
					mCurrentHotelSearchCondition);
			intent.putExtra("Hotel", mHotel);
			intent.putExtra("prepaid", mPrepaidFlag);
			intent.putExtra("SortByDistance", mSortByDistance);
			intent.putExtra("DestGeoPoint", mDestGeoPoint);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		}
		startActivityForResult(intent, 4);
	}

	@Override
	protected void titleLeftOnClick(String textString) {
		finish();
	}

	@Override
	protected void onFailure(int statusCode, Header[] headers,
			byte[] responseBody, Throwable error) {
		super.onFailure(statusCode, headers, responseBody, error);

		mHandler.sendEmptyMessage(1);

	}

	private Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case 0: {

				if (null != hotelNameSearchResult) {
					mHotelNameTextView.setVisibility(View.VISIBLE);
					mHotelNameLayout.setVisibility(View.VISIBLE);

					String nameString = hotelNameSearchResult.hotelName;
					if (TextUtils.isEmpty(nameString)) {
						nameString = hotelNameSearchResult.districtsname;
					}

					if (TextUtils.isEmpty(nameString)) {
						nameString = hotelNameSearchResult.bussinename;
					}

					if (TextUtils.isEmpty(nameString)) {
						nameString = hotelNameSearchResult.name;
					}

					if (TextUtils.isEmpty(nameString)) {
						nameString = hotelNameSearchResult.shortname;
					}
					mHotelNameTextView.setText(nameString);
					// mSearchTextView.setVisibility(View.INVISIBLE);
					mSearchTopLayout.setVisibility(View.INVISIBLE);

				} else if (!TextUtils
						.isEmpty(mCurrentHotelSearchCondition.keyword)) {
					mHotelNameTextView.setVisibility(View.VISIBLE);
					mHotelNameLayout.setVisibility(View.VISIBLE);
					mHotelNameTextView
							.setText(mCurrentHotelSearchCondition.keyword);
					// mSearchTextView.setVisibility(View.INVISIBLE);
					mSearchTopLayout.setVisibility(View.INVISIBLE);
				} else {
					mHotelNameTextView.setVisibility(View.GONE);
					mHotelNameLayout.setVisibility(View.GONE);
					if ("".equals(mCurrentHotelSearchCondition.keyword)) {
						mHotelNameTextView
								.setText(mCurrentHotelSearchCondition.keyword);
					}
					// mSearchTextView.setVisibility(View.INVISIBLE);
					mSearchTopLayout.setVisibility(View.VISIBLE);
				}

				if (null != mHotelSearchResultAdapter)
					mHotelSearchResultAdapter.notifyDataSetChanged();

				// mListView.getRefreshableView().setSelection(
				// mListView.getRefreshableView()
				// .getFirstVisiblePosition() + 1);
				mHandler.sendEmptyMessage(1);
				// HandlerToastUI.getHandlerToastUI(mContext, "加载完成");

				boolean sortByDistance = (Boolean) msg.obj;
				if (sortByDistance) {
					// 修改排序，按照距离排序
					if (-1 == mCurrentHotelSearchCondition.sorts) {
						mCurrentHotelSearchCondition.sorts = 4;
					}
					mHotelSort = mApplication.mHotelLocSorts;
				} else {
					// 默认排序
					// mCurrentHotelSearchCondition.sorts = 0;
					mHotelSort = mApplication.mHotelSorts;
				}

				if (mHotel.mHotelDocs.size() > 0) {
					haveResult();
				} else {
					noResult();
				}
				break;
			}
			case 1: {
				mListView.onRefreshComplete();
				break;
			}
			default:
				break;
			}
			return false;
		}
	});

}
